package admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import dtos.UserDTO;
import utils.UserDAORemote;

@ManagedBean
@SessionScoped
public class DecideReplacerBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	List<UserDTO> toReplaceUserList;
	List<UserDTO> officeManagersList;
	List<UserDTO> replacedUserList;
	@EJB
	UserDAORemote userDAO;

	String nameOM;
	UserDTO userDTO;
	UserDTO omDTO;
	String omId;
	String repId;
	boolean avemManageriActivi;
	boolean amTerminatTotul;
	
	@PostConstruct
	public void init() {
		repId = "0";
	}
	

	public boolean isAmTerminatTotul() {
		officeManagersList = getAllOfficeManagers();
		toReplaceUserList = getAllUsers();
		boolean b = false;
		if(officeManagersList.isEmpty() && toReplaceUserList.isEmpty()) b = true;
		if(officeManagersList.size()>0 || toReplaceUserList.size()>0) b = false;
		return b;
	}

	public void setAmTerminatTotul(boolean amTerminatTotul) {
		this.amTerminatTotul = amTerminatTotul;
	}

	public boolean isAvemManageriActivi() {
		officeManagersList = getAllOfficeManagers();
		if(officeManagersList == null) return false;
		if(officeManagersList.size()==0) return false;
		return true;
	}

	public void setAvemManageriActivi(boolean avemManageriActivi) {
		this.avemManageriActivi = avemManageriActivi;
	}

	public String getRepId() {
		return repId;
	}

	public void setRepId(String repId) {
		this.repId = repId;
	}

	public String getOmId() {
		return omId;
	}

	public void setOmId(String omId) {
		this.omId = omId;
	}

	public String getNameOM() {
		return nameOM;
	}

	public void setNameOM(String nameOM) {
		this.nameOM = nameOM;
	}

	public UserDTO getUserDTO() {
		return userDTO;
	}

	public void setUserDTO(UserDTO userDTO) {
		this.userDTO = userDTO;
	}

	public List<UserDTO> getOfficeManagersList() {
		officeManagersList = getAllOfficeManagers();
		return officeManagersList;
	}

	public void setOfficeManagersList(List<UserDTO> officeManagersList) {
		this.officeManagersList = officeManagersList;
	}

	public List<UserDTO> getToReplaceUserList() {
		toReplaceUserList = getAllUsers();
		return toReplaceUserList;
	}

	public void setToReplaceUserList(List<UserDTO> toReplaceUserList) {
		this.toReplaceUserList = toReplaceUserList;
	}

	public List<UserDTO> getReplacedUserList() {
		if(replacedUserList==null){
			replacedUserList= new ArrayList<>();
		}
		replacedUserList = getAllReplacedOfficeManagers();
		return replacedUserList;
	}

	public void setReplacedUserList(List<UserDTO> replacedUserList) {
		this.replacedUserList = replacedUserList;
	}

	/*
	 * getReplacedUserList
	 */
	public List<UserDTO> getAllReplacedOfficeManagers() {
		return userDAO.getAllReplacedOfficeManagers();
	}

	/*
	 * getAllOfficeManagersToBeReplaced
	 */
	public List<UserDTO> getAllUsers() {
		return userDAO.getInactiveOfficeManagers();
	}

	/*
	 * getAllOfficeManagersToBeThePossibleReplacers
	 */
	public List<UserDTO> getAllOfficeManagers() {
		userDTO = userDAO.getUser(Integer.valueOf(repId));
		return userDAO.getActiveOfficeManagers(userDTO);
	}

	public void performReplace(ActionEvent event) {
		/*this.userDTO = (UserDTO) event.getComponent().getAttributes()
				.get("inactiveOfficeManager");*/
		userDTO = userDAO.getUser(Integer.valueOf(repId));
		omDTO = userDAO.getUser(Integer.valueOf(omId));
		userDTO.setIdUser(omDTO.getId());
		userDAO.updateUser(userDTO);
		toReplaceUserList = getAllUsers();
		officeManagersList = getAllOfficeManagers();
		this.userDTO = null;
	}

	public void updateReplacerId(ValueChangeEvent event) {
		omId = (String) event.getNewValue();
	}
	
	public void updateReplacedId(ValueChangeEvent event) {
		if(event.getNewValue() instanceof String)
			repId = (String) event.getNewValue();
		try{
		userDTO = userDAO.getUser(Integer.valueOf(repId));
		} catch(NumberFormatException e){
			
		}
	}
	
	public UserDTO getOM(int id){
		return userDAO.getUser(userDAO.getUser(id).getIdUser());
	}

	public void replacementDone(ActionEvent event) {
		repId="-1";
		this.userDTO = (UserDTO) event.getComponent().getAttributes()
				.get("replacedUser");
		userDTO.setIdUser(0);
		userDTO.setIsActive(1);
		userDAO.insertUser(userDTO);
		toReplaceUserList = getAllUsers();
		officeManagersList = getAllOfficeManagers();
		this.userDTO = null;
	}
	

}
